<div class="container-xl">
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          电视剧订阅
        </h2>
      </div>
      <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:show_add_rss_media_modal()" class="btn btn-primary d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
            新增订阅
          </a>
          <a href="javascript:show_add_rss_media_modal()" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></svg>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
{% if Count > 0 %}
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      {% for Item in Items %}
      <div class="col-12 col-md-4 col-xl-3">
        <a class="card card-link card-link-pop rounded-3" href='javascript:{% if Item[3] %}show_mediainfo_modal("TV", "{{ Item[0] }}", "{{ Item[1] }}", "{{ Item[3] }}", "tv_rss", "{{ Item[10] }}"){% else %}show_edit_rss_media_modal("{{ Item[10] }}"){% endif %}'>
          <div class="text-center" style="overflow:hidden">
            <img class="rounded-top w-100" src="{{ Item[4] }}" style="border-top-left-radius:8px !important; border-top-right-radius:8px !important; object-fit:cover; max-height:170px;">
          </div>
          <div class="card-progress">
            <div class="progress-bar bg-green" style="width: {{ Item[9] or 0 }}%" role="progressbar" aria-valuenow="{{ Item[9] or 0 }}" aria-valuemin="0" aria-valuemax="100">
              <span class="visually-hidden">{{ Item[9] }}%</span>
            </div>
          </div>
          <div class="card-body text-center">
            <div class="card-title mb-1">
              {% if Item[1] %}
              {{ Item[0] }} ({{ Item[1] }}) {% if Item[2] != "S00" %}{{ Item[2] }}{% endif %}
              {% else %}
              {{ Item[0] }} {% if Item[2] != "S00" %}{{ Item[2] }}{% endif %}
              {% endif %}
            </div>
            <div class="text-muted">
              <span class="badge
              {% if Item[8] == 'D' %}
              bg-gray
              {% elif Item[8] == 'S' %}
              bg-orange
              {% elif Item[8] == 'R' %}
              bg-green
              {% else %}
              bg-blue
              {% endif %}
              "></span>
              {% if Item[8] == "D" %}
              队列中
              {% elif Item[8] == "S" %}
              正在搜索
              {% elif Item[8] == "R" %}
              正在订阅 {% if Item[6] > 0 %}({{ Item[6] - Item[7] }}/{{ Item[6] }}){% endif %}
              {% else %}
              完成
              {% endif %}
            </div>
            {% if Item[5]|rss_filter_string %}
            <div class="text-muted border-top pt-2 mt-2">
              <small>{{ Item[5]|rss_filter_string|safe }}</small>
            </div>
            {% endif %}
            {% if Item[5]|rss_sites_string %}
            <div class="text-muted {% if not Item[5]|rss_filter_string %}border-top pt-2 mt-2{% endif %}">
              <small>{{ Item[5]|rss_sites_string|safe }}</small>
            </div>
            {% endif %}
          </div>
        </a>
      </div>
      {% endfor %}
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/sign_in.svg" height="128"  alt="">
      </div>
      <p class="empty-title">没有记录</p>
      <p class="empty-subtitle text-muted">
        当前没有正在订阅的电视剧。
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-tv-rss" tabindex="-1" role="dialog" aria-hidden="true" data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="card border-top-0">
        <div class="card-header">
          <h5 class="modal-title" id="tv_rss_modal_title">新增订阅</h5>
          <input type="hidden" id="tv_rssid">
          <input type="hidden" id="tv_tmdbid">
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="card-body">
          <div class="row">
            <label class="form-label required">电视剧</label>
            <div class="row mb-3">
              <div class="col-12 col-lg-6 mb-1">
                <input type="text" value="" id="tv_name" class="form-control" placeholder="标题">
              </div>
              <div class="col-12 col-lg-3 mb-1">
                <input type="text" value="" id="tv_year" class="form-control" placeholder="年份">
              </div>
              <div class="col-12 col-lg-3 mb-1">
                <select class="form-select" id="tv_season">
                  <option value="" selected>请选择</option>
                </select>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-xl-4">
              <div class="mb-3">
                <label class="form-check form-switch">
                  <input class="form-check-input" type="checkbox" id="key_match" onclick="javascript:change_match_check(this)">
                  <span class="form-check-label">模糊匹配 <span class="form-help bg-red-lt" title="开启后不检查TMDB是否有媒体信息，只要种子名称、标题、年份任一匹配关键字即会下载，此时标题可以配置正则表达式实现模糊匹配" data-bs-toggle="tooltip">?</span></span>
                </label>
              </div>
            </div>
            <div class="col-xl-4">
              <div class="mb-3">
                <label class="form-check form-switch">
                  <input class="form-check-input" type="checkbox" id="over_edition" onclick="javascript:change_over_edition_check(this)">
                  <span class="form-check-label">洗版 <span class="form-help bg-red-lt" title="开启后RSS不检查媒体库是否已存在，命中即会下载" data-bs-toggle="tooltip">?</span></span>
                </label>
              </div>
            </div>
          </div>
          <div class="row">
            <label class="form-label">过滤规则</label>
            <div class="row mb-3">
              <div class="col-12 col-lg-4 mb-1">
                <select class="form-select" id="rss_restype">
                  <option value="" selected>全部</option>
                  {% for restype in RestypeDict %}
                  <option value="{{ restype }}">{{ restype }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="col-12 col-lg-4 mb-1">
                <select class="form-select" id="rss_pix">
                  <option value="" selected>全部</option>
                  {% for pix in PixDict %}
                  <option value="{{ pix }}">{{ pix }}</option>
                  {% endfor %}
                </select>
              </div>
              <div class="col-12 col-lg-4 mb-1">
                <select class="form-select" id="rss_rule">
                  <option value="" selected>请选择</option>
                  {% for RuleGroup in RuleGroups %}
                  <option value="{{ RuleGroup.id }}">{{ RuleGroup.name }}</option>
                  {% endfor %}
                </select>
              </div>
            </div>
          </div>
          <div class="row rss_sites_container" id="rss_sites_div">
            <div class="mb-3">
              <label class="form-label">订阅站点</label>
              <div class="form-selectgroup">
                {% for site in Sites %}
                 <label class="form-selectgroup-item">
                  <input type="checkbox" name="sites" value="{{ site.name }}" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">{{ site.name }}</span>
                </label>
                {% endfor %}
              </div>
            </div>
          </div>
          <div class="row rss_sites_container" id="rss_search_sites_div">
            <div class="mb-3">
              <label class="form-label">搜索站点</label>
              <div class="form-selectgroup">
                {% for site in SearchSites %}
                 <label class="form-selectgroup-item">
                  <input type="checkbox" name="search_sites" value="{{ site }}" class="form-selectgroup-input" checked>
                  <span class="form-selectgroup-label">{{ site }}</span>
                </label>
                {% endfor %}
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <a href="javascript:rss_sites_select()" class="btn btn-link me-auto" id="rss_sites_select_btn">
          取消全选
        </a>
        <a href="#" class="btn btn-link" id="rss_delete_btn">
          删除订阅
        </a>
        <a href="javascript:add_rss_tv()" id="rss_add_btn" class="btn btn-primary">确定</a>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  //新增订阅
  function add_rss_tv(){
    var rssid = $("#tv_rssid").val();
    var tmdbid = $("#tv_tmdbid").val();
    var rssid = $("#tv_rssid").val();
    var name = $("#tv_name").val();
    var year = $("#tv_year").val();
    var season = $("#tv_season").val();
    var match = $("#key_match").prop("checked");
    var rss_restype = $("#rss_restype").val();
    var rss_pix = $("#rss_pix").val();
    var rss_rule = $("#rss_rule").val();
    var over_edition = $("#over_edition").prop("checked");
    if (!name) {
      $("#tv_name").addClass("is-invalid");
      return;
    } else {
      $("#tv_name").removeClass("is-invalid");
    }
    if (year && isNaN(year)) {
      $("#tv_year").addClass("is-invalid");
      return;
    } else {
      $("#tv_year").removeClass("is-invalid");
    }
    if(!match && !season){
      $("#tv_season").addClass("is-invalid");
      return;
    }else{
      $("#tv_season").removeClass("is-invalid");
    }
    if(tmdbid && tmdbid.startsWith("DB:")){
      var doubanid = tmdbid.replace("DB:", "");
      tmdbid = "";
    }else{
      var doubanid = "";
    }
    //订阅站点
    var sites = [];
    var i = 0;
    var checkall = true;
    $("input[name=sites]").each(function(){
      if($(this).prop("checked")){
        sites[i] = $(this).val();
        i = i + 1;
      }else{
        checkall = false;
      }
    });
    if(checkall){
      sites = [];
    }
    //搜索站点
    var search_sites = [];
    if(!match){
      var i = 0;
      var search_checkall = true;
      $("input[name=search_sites]").each(function(){
        if($(this).prop("checked")){
          search_sites[i] = $(this).val();
          i = i + 1;
        }else{
          search_checkall = false;
        }
      });
      if(search_checkall){
        search_sites = [];
      }
    }
    var data = { "name": name,
                 "type": "TV",
                 "year": year,
                 "season": season,
                 "match": match,
                 "sites": sites,
                 "search_sites": search_sites,
                 "rss_restype": rss_restype,
                 "rss_pix": rss_pix,
                 "rss_rule": rss_rule,
                 "over_edition": over_edition,
                 "rssid": rssid,
                 "tmdbid": tmdbid,
                 "doubanid": doubanid};
    $("#modal-tv-rss").modal("hide");
    show_wait_process();
    ajax_post("add_rss_media", data, function(ret){
      hide_wait_process();
      if(ret.code==0){
        navmenu("tv_rss");
      }else{
        show_fail_modal(ret.name + " 订阅失败："+ ret.msg +"！");
      }
    });
  }

  //初始化下拉框
  $("#tv_season").empty();
  $("#tv_season").append('<option value="">请选择</option>');
  for(var i=1; i<=50; i++){
    $("#tv_season").append('<option value="' + i + '">第' + i +'季</option>');
  }

  //取消订阅
  function remove_rss_tv(name, year, rssid){
    $("#modal-tv-rss").modal('hide');
    show_ask_modal("是否确认取消订阅 " + name + "？", function(){
      hide_ask_modal();
      remove_rss_media(name, year, "TV", rssid, "tv_rss");
    });
  }

  //RSS站点批量选择
  rss_sites_select_all = true;
  function rss_sites_select(){
    if(rss_sites_select_all){
      // 取消全选
      $(".rss_sites_container").find("input[type=checkbox]").each(function(){
        $(this).prop("checked",false);
      });
      rss_sites_select_all = false;
      $("#rss_sites_select_btn").text("全选");
    }else{
      //全选
      $(".rss_sites_container").find("input[type=checkbox]").each(function(){
        $(this).prop("checked",true);
      });
      rss_sites_select_all = true;
      $("#rss_sites_select_btn").text("取消全选");
    }
  }

  // 选择模糊匹配
  function change_match_check(obj){
    if($(obj).prop("checked")){
      $("#rss_search_sites_div").hide();
      $("#over_edition").prop("checked", false);
    }else{
      $("#rss_search_sites_div").show();
    }
  }

  // 选择洗版
  function change_over_edition_check(obj){
    if($(obj).prop("checked")){
      $("#key_match").prop("checked", false);
      $("#rss_search_sites_div").show();
    }
  }

  // 显示新增订阅
  function show_add_rss_media_modal(){
    $("#tv_rssid").val("");
    $("#tv_tmdbid").val("");
    $("#tv_rss_modal_title").text("新增订阅");
    $("#key_match").prop("checked", false);
    $("#rss_search_sites_div").show();
    $("#over_edition").prop("checked", false);
    $("#tv_name").attr("readonly", false);
    $("#tv_year").attr("readonly", false);
    $("#modal-tv-rss").modal('show');
  }

  // 显示编辑订阅
  function show_edit_rss_media_modal(rssid){
    $("#system-media-modal").modal('hide');
    $("#tv_rssid").val(rssid);
    $("#tv_rss_modal_title").text("编辑订阅");
    show_wait_process();
    ajax_post("rss_detail", {"rssid": rssid, "rsstype": "TV"}, function(ret){
      hide_wait_process();
      if(ret.code==0){
        $("#tv_tmdbid").val(ret.detail.tmdbid);
        $("#tv_name").val(ret.detail.name);
        if(ret.detail.season){
          $("#tv_season").val(parseInt(ret.detail.season.replace("S", "")));
        }else{
          $("#tv_season").val("");
        }
        $("#tv_name").attr("readonly", true);
        $("#tv_year").val(ret.detail.year);
        $("#tv_year").attr("readonly", true);
        if(ret.detail.tmdbid){
          $("#key_match").prop("checked", false);
          $("#rss_search_sites_div").show();
        }else{
          $("#key_match").prop("checked", true);
          $("#rss_search_sites_div").hide();
          $("#over_edition").prop("checked", false);
        }
        if(ret.detail.over_edition){
          $("#over_edition").prop("checked", true);
        }else{
          $("#over_edition").prop("checked", false);
        }
        $("#rss_restype").val(ret.detail.filter.restype);
        $("#rss_pix").val(ret.detail.filter.pix);
        $("#rss_rule").val(ret.detail.filter.rule);
        $("#rss_sites_div").find("input[type=checkbox]").each(function(){
          if(ret.detail.r_sites.length==0 || ret.detail.r_sites.includes($(this).val())){
            $(this).prop("checked",true);
          }else{
            $(this).prop("checked",false);
          }
        });
       $("#rss_search_sites_div").find("input[type=checkbox]").each(function(){
          if(ret.detail.s_sites.length==0 || ret.detail.s_sites.includes($(this).val())){
            $(this).prop("checked",true);
          }else{
            $(this).prop("checked",false);
          }
        });
        $("#rss_delete_btn").attr("href", 'javascript:remove_rss_tv("'+ret.detail.name+'","'+ret.detail.year+'","'+rssid+'")');
        $("#modal-tv-rss").modal('show');
      }
    });
  }

</script>